package measures;

import java.io.*;
import java.util.*;

/**
 * @author Sabrina Mänz, sabrinamaenz@googlemail.com
 * Translation Memories for patent translation
 * InputWriter 
 * Writes the fuzzyMatches via hadoop-outputfile in one file.
 * The output contains the testset-sentence followed by the trainset-sentence 
 * followed by the appendant alignment
 */
public class InputWriter {
	
	/**
	 * Reads the files.
	 * @param text input-file as String
	 * @return ArrayList<String> containing the content of the given text-file
	 * @throws IOException if the file not exists
	 */
	public static ArrayList<String> reading(String text) throws IOException{
		ArrayList<String> s = new ArrayList<String>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
				    new FileInputStream(text), "UTF-8"));
			for(String line = br.readLine(); line!=null; line = br.readLine()){
				s.add(line);
			}
			br.close();
			System.out.println("File found.");
		}
		catch(IOException e){
			System.err.println("Didn´t find the file!");
		}
		
		return s;
	}
	
	/**
	 * @param hadoop: hadoop-output e.g.: 1001 3573116 0.85 
	 * the first number is the sentence number in the testset
	 * the second number is the sentence number of the fuzzyMatch out of the trainset
	 * the third number is the fuzzyMatchScore (>0.7)
	 * @param source: sentences out of testset
	 * @param target: sentences out of trainingsset
	 * @param output: contains the testset-sentence followed by the trainset-sentence 
	 * followed by the appendant alignment
	 * @throws IOException if the file can´t be written
	 */
	public static void concatenate(String hadoop, String source, String target, String output) throws IOException{
		
		File file = new File(output);
		
		Writer writer = new BufferedWriter(new OutputStreamWriter(
			    new FileOutputStream(file), "UTF-8"));
		
		
		try{
			//writer = new FileWriter(file ,true);
		
		ArrayList<String> comparison = new ArrayList<String>();
		comparison = reading(hadoop);
		ArrayList<String> test = new ArrayList<String>();
		test = reading(source);
		ArrayList<String> train = new ArrayList<String>();
		train = reading(target);
		
		
		//Iterieren über vergleich, extrahieren der beiden Zahlen am Anfang
		String[] vergleichValues = new String[3];
		String testNumb = "";
		String trainNumb = "";
		//iteriert über vergleichsdatei
			for (int i=0; i<comparison.size(); i++){
				String testAndTrain = "";
				vergleichValues = comparison.get(i).split("\t");
				//extrahiert Nummern als testValue und trainValue
				String testValue = vergleichValues[0];
				String trainValue = vergleichValues[1];
				//soll entsprechende Zeilen aus test rausfischen
				for(int j=0; j<test.size(); j++){
					//splittet an <s>, splitting an pipe problematisch
					String[] testSplit = test.get(j).split("<s>");
					//deswegen nochmal splitting an |||
					testNumb = testSplit[0].replace("|||", "");
					
					if(testValue.equals(testNumb)){
						//System.out.println(test.get(j));
						testAndTrain += test.get(j);
						testAndTrain += "\t";
						break;
					}
				}
				for(int k=0; k<train.size(); k++){
					String[] trainSplit = train.get(k).split("<s>");
					trainNumb = trainSplit[0].replace("|||", "");
					if(trainValue.equals(trainNumb)){
						//System.out.println(train.get(k));
						testAndTrain += train.get(k)+"\n";
						
						break;
					}
				}
				writer.write(testAndTrain);
			}
			writer.flush();
			writer.close();
			System.out.println(output);
			System.out.println("file written");
		}
		
		catch(IOException e){
			e.printStackTrace();
			System.err.println("Writing process failed!");
		}
	}
	
	/**
	 * Calls the method concatenate with the given arguments from the command-line.
	 * @param args should contain four arguments
	 * @throws IOException if less than four arguments given
	 */
	public static void main(String[] args) throws IOException{

		
		try{
//			String[] args1 = new String[4];
		
//			args1[0] = "/home/students/wangler/hadoop.out2";
//			args1[1] = "/home/students/wangler/pattr.xml.in.part1";
//			args1[2] = "/home/students/wangler/pattr.xml.in.part2";
//			args1[3] = "/home/students/wangler/output.inputwriter";
						
			String hadoop = args[0];
			String source = args[1];
			String target = args[2];
			String output = args[3];
		
		
			InputWriter.concatenate(hadoop, source, target, output);
		
		}catch(Exception e){
			System.err.println("Incorrect Input, please use 4 parameters containing the hadoopfile, " +
					"the sourcefile, the targetfile and a designated outputfile");
		}
	}
		
}
